package com.xc.ezplan.auth.service.impl;

import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import com.xc.ezplan.auth.feign.AdminUserFeignService;
import com.xc.ezplan.auth.service.AuthUserService;
import com.xc.ezplan.core.base.EzResult;
import com.xc.ezplan.core.dto.AdminUserDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author lixingchen
 * @date 2022/11/15
 * @description 用户权限相关实现类
 */
@Service
@RequiredArgsConstructor
public class AuthUserServiceImpl implements AuthUserService {

	private final AdminUserFeignService adminUserFeignService;

	@Override
	public SaTokenInfo login(String username, String password) {
		EzResult<AdminUserDTO> adminUserDTOResult = adminUserFeignService.queryUserByUserName(username);
        AdminUserDTO adminUserDTO = adminUserDTOResult.getData();
        if (Objects.isNull(adminUserDTO)){
            return null;
        }
        if (password.equals(adminUserDTO.getPassword())){
            // 密码校验成功后登录，一行代码实现登录
            StpUtil.login(adminUserDTO.getId());
            // 将用户信息存储到Session中
            StpUtil.getSession().set("userInfo", adminUserDTOResult.getData());
            // 获取当前登录用户Token信息
            return StpUtil.getTokenInfo();
        }
		return null;
	}
}
